package antlr.calc;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;

public class CalcMain {
    public static void main(String[] args) {
        replaceBracket();
    }

    static void calc() {
        String expr= "(123 +456 ) * (600/6)  -100";
        ANTLRInputStream input = new ANTLRInputStream(expr);  //将输入转成antlr的input流
        CalcLexer lexer = new CalcLexer(input);  //词法分析
        CommonTokenStream tokens = new CommonTokenStream(lexer);  //转成token流
        CalcParser parser = new CalcParser(tokens); // 语法分析
        CalcParser.ExprContext tree = parser.expr();  //获取某一个规则树，这里获取的是最外层的规则，也可以通过sql()获取sql规则树......
        MyCalcVisitor visitor = new MyCalcVisitor();
        Integer result = visitor.visit(tree);
        System.out.println(visitor.calcCnt);
        System.out.println(result);
    }

    static void replaceBracket() {
        String expr= "(123 +456 ) * (600/6)  -100";
        ANTLRInputStream input = new ANTLRInputStream(expr);  //将输入转成antlr的input流
        CalcLexer lexer = new CalcLexer(input);  //词法分析
        CommonTokenStream tokens = new CommonTokenStream(lexer);  //转成token流
        CalcParser parser = new CalcParser(tokens); // 语法分析
        CalcParser.ExprContext tree = parser.expr();  //获取某一个规则树，这里获取的是最外层的规则，也可以通过sql()获取sql规则树......
        MyReplaceVisitor visitor = new MyReplaceVisitor(tokens);
        Integer result = visitor.visit(tree);
        System.out.println(visitor.rewriter.getText());
    }
}
